DialogMgr={};

local DialogDic={};
local mainDialog=nil;
local showDialogDic={};

function DialogMgr:show(dialog)
    local d = nil;
    for i,v in ipairs(DialogDic) do
        if v == dialog then
            d = dialog;
        end
    end
    if d~=nil and mainDialog==d then
        return ;
    end
    if d==nil then
        table.insert(DialogDic,dialog);
        d=dialog;
    end
    mainDialog=d;
    table.insert(showDialogDic,d);
    UIRoot.dialogMask.gameObject:SetActive(true);
    UIRoot.dialogMask:SetAsLastSibling();
    TimeMgr:removeDelayFun(d.delayDestroyHandler);
    d:setVisible(true);
    if not d.isInit then
        d:setParent(UIRoot.dialog);
        d:loadPrefab();
    else 
        d.gameObject.transform:SetAsLastSibling();
    end
end

function DialogMgr:hide(dialog)
    local d = dialog;
    if d==nil then
        return ;
    end
    d:setVisible(false);
    for i,v in ipairs(showDialogDic) do
        if v == d then
            table.remove(showDialogDic, i);
        end
    end
    if #showDialogDic==0 then
        mainDialog=nil;
        UIRoot.dialogMask.gameObject:SetActive(false);
    else
        mainDialog=showDialogDic[#showDialogDic];
        UIRoot.dialogMask.gameObject:SetActive(true);
        UIRoot.dialogMask:SetAsLastSibling();
        mainDialog.gameObject.transform:SetAsLastSibling();
    end

    d.delayDestroyHandler=TimeMgr:delayFun(10,function ()
        d:destroy();
    end);
end

function DialogMgr:getMainDialog()
    return mainDialog;
end